package pegpeg.client;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;

import java.util.ArrayList;
import java.util.Random;

public class PegGroup {
	double width;
	double height;
	int triangleLength = 500;
	ArrayList<Peg> pegsArrayList;
	
	// color and dark color
	final String[] co = {"#FF0000", "#DA70D6", "#008000", "#FFD700", "#0000FF"};
	final String[] dc = {"#8B0000", "#9932CC", "#006400", "#B8860B", "#00008B"};
	Random generator = new Random();
	Peg[] pegs;
	int r1, c1, r2, c2, p2;
	int moving_peg = -1;
	boolean midgame;

	public PegGroup(PegGroup another){
		this.width = another.width;
		this.height = another.height;
		this.pegsArrayList = new ArrayList<Peg>();
		this.midgame = another.midgame;
		for(int i = 0; i<another.pegsArrayList.size(); i++){
			Peg temp = another.pegsArrayList.get(i);
			this.pegsArrayList.add(new Peg(temp));
		}
		this.pegs = new Peg[pegsArrayList.size()];
		for(int i = 0; i<pegsArrayList.size(); i++){
			Peg peg =  this.pegsArrayList.get(i);
			this.pegs[i] = peg;
		}
	}
	
	public PegGroup(double width, double height) {
		this.width = width;
		this.height = height;
		midgame = false;
		double pt = height/2 - Math.sqrt(3)*triangleLength*0.5*0.5;
		double a = triangleLength/12;

		pegsArrayList = new ArrayList<Peg>();
		for(int i = 0; i<5; i++){
			for(int j = 0; j<=i; j++){
				int k = generator.nextInt(5);
				double px = width/2 - (i+1)/6.0*triangleLength/2 + a + j*2*a;
				double py = pt + (i+1)*Math.sqrt(3)*a;
				pegsArrayList.add(new Peg(i, j, px, py, co[k], dc[k]));
			}
		}
		pegs = new Peg[pegsArrayList.size()];
		for (int i = pegsArrayList.size() - 1; i >= 0; i--) {
			Peg peg = pegsArrayList.get(i);
			pegs[i] = peg;
		}
	}


	public boolean update(int mouseX, int mouseY) {
		boolean newmove = false;
		if(!midgame){
			for(int i = 0; i<pegs.length; i++) {
				if (pegs[i].check(mouseX, mouseY)) {
					pegs[i].hasPeg = false;
					midgame = true;
					newmove = true;
				}
			}
		}
		for (int i = 0; i<pegs.length; i++) {
			if (!pegs[i].check(mouseX, mouseY))
				continue;

			if (moving_peg>=0) {
				if (pegs[i].hasPeg == true) {
					pegs[moving_peg].dark = false;
					moving_peg = -1;
				} else {
					p2 = i;
					r1 = pegs[moving_peg].row;	
					c1 = pegs[moving_peg].col;					
					r2 = pegs[i].row;
					c2 = pegs[i].col;
					if(valid(r1, r2, c1, c2)){
						pegs[moving_peg].hasPeg = false;
						pegs[p2].hasPeg = true;
						pegs[p2].color = pegs[moving_peg].color;
						pegs[p2].darkc = pegs[moving_peg].darkc;
						pegs[p2].dark = false;
						moving_peg = -1;
						newmove = true;
					}
				}
			} else {
				if (pegs[i].hasPeg == true) {
					moving_peg = i;
					pegs[i].dark = true;
				}
			}
			break;
		}
		return newmove;
	}

	private boolean valid(int r11, int r22, int c11, int c22) {
		int rmin = Math.min(r11, r22);
		int rmax = Math.max(r11, r22);
		int cmin = Math.min(c11, c22);
		int cmax = Math.max(c11, c22);
		if ((rmin == rmax && (cmax-cmin)==2) ||
			(cmin == cmax && (rmax-rmin) ==2) ||
			((cmax-cmin ==2) &&(rmax-rmin)==2)) {
			int rmid = (rmin+rmax)/2;
			int cmid = (cmin+cmax)/2;
			int mid = (1+rmid)*rmid/2+cmid;
			if (pegs[mid].hasPeg == true) {
				pegs[mid].hasPeg = false;
				return true;
			}
		}
		return false;
	}


	public void draw(Context2d context) {
		for (int i = pegs.length-1; i>=0; i--) {
			pegs[i].draw(context);
		}
	}
}